package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Choujiang;
import com.qianniu.napi.admin.entity.CouponTpl;
import com.qianniu.napi.admin.entity.Goods;
import com.qianniu.napi.admin.entity.penum.Jptype;
import com.qianniu.napi.admin.service.IChoujiangService;
import com.qianniu.napi.admin.service.ICouponTplService;
import com.qianniu.napi.admin.service.IGoodsCategoryService;
import com.qianniu.napi.admin.service.IGoodsService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2018-12-14
 */
@Controller
public class ChoujiangController {

    private static final Logger logger = LoggerFactory.getLogger(ChoujiangController.class);

    @Autowired
    private IChoujiangService choujiangService;
    @Autowired
    private ICouponTplService couponTplService;
    @Autowired
    private IGoodsService goodsService;


    @RequestMapping("admin/choujiang/index")
    public String index(Model model) {
        List<CouponTpl> ctpl = couponTplService.getMechantCouponstpl(ObjectUtil.getMpid());
        List<Goods> goods = goodsService.selectList(new EntityWrapper<Goods>()
                .setSqlSelect("id,name,pic")
                .eq("mpid", ObjectUtil.getMpid())
                .orderBy("createtime", false)
        );
        model.addAttribute("couponTpls", ctpl);
        model.addAttribute("goods", goods);

        return "admin/choujiang";
    }

    @RequestMapping("/admin/choujiang/list")
    @ResponseBody
    public Page<Choujiang> list(@RequestParam(value = "offset", required = false) Integer offset,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "search", required = false) String search,
                                @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Choujiang> wrapper = new EntityWrapper<Choujiang>();
        int utype = ObjectUtil.getUserType();
        if (utype < 39)
            wrapper.eq("mpid", ObjectUtil.getMpid());
        else if (utype < 99)
            wrapper.eq("mid", ObjectUtil.getMid());
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("order_no", search).or().like("remark", search);

        wrapper.orderBy("sort", true);
        Page<Choujiang> page = choujiangService.selectPage(new Page<Choujiang>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/choujiang/add")
    public String add(@Valid Choujiang bean, BindingResult bindingResult,
                      @RequestParam(value = "ywid", required = false) Long ywid,Model model) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        logger.info("==============================ywid="+ywid);
        logger.info("==============================bean="+bean);

        if(bean.getJtype()== Jptype.quan.getVal()&&ywid!=null&&ywid>0){
            CouponTpl ctpl = couponTplService.selectById(ywid);
            if(ctpl!=null)bean.setValue(ctpl.getCost().intValue());
            bean.setPic("https://img.niucaiduo.com/vmall/yx/quan.jpg");
        }else if(bean.getJtype()== Jptype.shanpin.getVal()&&ywid!=null&&ywid>0){
            logger.debug("=====================ywid="+ywid);
            Goods goods = goodsService.selectById(ywid);
            logger.debug("=====================goods="+goods);

            if(goods!=null)bean.setValue(goods.getPrice().intValue());
            if(Strings.isNullOrEmpty(bean.getPic()))bean.setPic(goods.getPic());
        }else if(bean.getJtype()== Jptype.xiexie.getVal()){
            bean.setName("谢谢参与");
        }else if(bean.getJtype()== Jptype.jifen.getVal()){
            bean.setPic("https://img.niucaiduo.com/vmall/yx/jifen.jpg");
        }else if(bean.getJtype()== Jptype.hongbao.getVal()){
            bean.setPic("https://img.niucaiduo.com/vmall/yx/hongbao.jpg");
        }

        bean.setJid(ywid);
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();

        List<CouponTpl> ctpl = couponTplService.getMechantCouponstpl(ObjectUtil.getMpid());
        List<Goods> goods = goodsService.selectList(new EntityWrapper<Goods>()
                .setSqlSelect("id,name,pic")
                .eq("mpid", ObjectUtil.getMpid())
                .orderBy("createtime", false)
        );

        model.addAttribute("couponTpls", ctpl);
        model.addAttribute("goods", goods);
        return "admin/choujiang";
    }

    @RequestMapping("/admin/choujiang/getChoujiangById")
    @ResponseBody
    public Choujiang getChoujiangById(long id) {
        Choujiang choujiang = choujiangService.selectById(id);
        return choujiang;
    }

    @RequestMapping("/admin/choujiang/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = choujiangService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/choujiang/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        Choujiang bean = new Choujiang();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        choujiangService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/choujiang/modify")
    public String update(Choujiang bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = choujiangService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }
}
